﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using YDWin.Base;
using YDServices;
using YDModel;
using DevExpress.Utils;
using YDCommon;
using YDServices.Enum; 
using static YDCommon.Utils;
using DevExpress.XtraTreeList.Nodes;

namespace YDWin.Role
{
    public partial class AddRoleForm : AddForm
    {
        RoleService svr = new RoleService();
        RolePermissionService rps = new RolePermissionService();
        PermissionService ps = new PermissionService();
        public T_Role editTu = new T_Role();
        public string state = "";
        public AddRoleForm()
        {
            InitializeComponent();
            //绑定treelist的checkbox的字段名
            trPermission.CheckBoxFieldName = "IsChecked"; 
 
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
 
            T_Role fact = new T_Role();
            if (!state.Equals("") && state.Equals("Edit"))
            {
                fact = editTu;
            }

            if (txtRoleName.Text.Trim().Equals(""))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "角色名称不能为空!\n"); 
                return;
            }

            fact.RoleName = txtRoleName.Text.Trim();
            //权限信息 
            List<T_Permission> perList = bsData.DataSource as List<T_Permission>;

            WaitDialogForm sdf = new WaitDialogForm("", "正在保存......");
            if (!state.Equals("") && state.Equals("Edit"))
            {
                svr.EditItem(fact, perList);
            }
            else
            {
                svr.AddItem(fact, perList);
            }
      

            sdf.Close();

            if (svr.HasError)
            {
                Utils.ShowPrompt(MessageType.mtError, "角色信息保存失败：" + svr.Errors);
                svr.Errors.Clear();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void AddRoleForm_Load(object sender, EventArgs e)
        {
            if (!state.Equals("") && state.Equals("Edit"))
            {
                //获取所有角色权限信息 
                var list = rps.getTreeData(editTu.ID);
                bsData.DataSource = list;

                txtRoleName.Text = editTu.RoleName;
            }
            else
            {
                //获取所有权限信息 
                var list = ps.getTreeData();
                bsData.DataSource = list; 
            }

            //默认展开
            trPermission.ExpandAll();

        }

        /// <summary>
        /// 选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trPermission_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            //如果有子节点,并且状态为选中,则选中所有的子节点
            if(e.Node.HasChildren)
            {
                if(e.Node.CheckState== CheckState.Checked)
                {
                    e.Node.CheckAll(); 
                }
                else
                {
                    e.Node.UncheckAll();
                }
            }

            //如果有父节点,并且状态为选中,则选中父节点
            if(e.Node.ParentNode!=null)
            {
                if (e.Node.CheckState == CheckState.Checked)
                {
                    e.Node.ParentNode.Checked = true;
                }
                else
                {
                    //如果父节点下的子节点都没有被选中,则父节点也取消选中
                    bool ischeck = false;
                    foreach (TreeListNode  child in e.Node.ParentNode.Nodes)
                    {
                        if(child.CheckState==CheckState.Checked )
                        {
                            ischeck = true;
                        }
                    }
                    if(!ischeck)
                    {
                        e.Node.ParentNode.Checked = false;
                    }

                }
            }

        }
          
    }
}